@extends('admin.base')

@section('content')
    <style>
        #regain {
            margin-: 10px;
        !important;
        }
    </style>

    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto layui-form">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-block">
                        <input type="text" id="display_name" placeholder="请输入" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-admin layui-btn-normal" id="search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn "></i>搜 索
                    </button>
                </div>
            </div>
        </div>
        <div class="layui-card-header layuiadmin-card-header-auto">
            <div class="layui-btn-group">
                @can('platform.platform.destroy')
                    <button class="layui-btn layui-btn-danger" id="listDelete" style="margin-right: 10px!important;">删
                        除
                    </button>
                @endcan
                @can('platform.platform.store')
                    <a class="layui-btn layui-btn-normal " id="addrole">添 加</a>
                @endcan
            </div>
        </div>

        <div class="layui-card-body">
            <table id="dataTable" lay-filter="dataTable"></table>
            <script type="text/html" id="options">
                <div class="layui-btn-group">
                    @can('platform.platform.update')
                        <a class="layui-btn layui-btn-sm layui-btn-normal" lay-event="edit"><i class="layui-icon">&#xe642;</i>编辑</a>
                    @endcan
                    @can('platform.platform.secret')
                        <a class="layui-btn layui-btn-warm layui-btn-sm" lay-event="regain"><i class="layui-icon">&#xe615;</i>查看secret</a>
                    @endcan
                </div>
            </script>
        </div>
    </div>

@endsection

@section('script')
    @can('platform.platform')
        <script>
            layui.use(['layer', 'table', 'form'], function () {
                var layer = layui.layer;
                var form = layui.form;
                var table = layui.table;
                var load = layer.load();
                var where = {
                    like:{},
                    sorts: {}
                }
                //用户表格初始化
                var dataTable = table.render({
                    elem: '#dataTable'
                    , height: 700
                    , url: "{{ route('platform.platform.data') }}" //数据接口
                    , where: where
                    , loading: false
                    , autoSort: false
                    , limits: [10, 15, 30]
                    , limit: 15
                    , page: {theme: '#1E9FFF'} //开启分页
                    , cols: [[ //表头
                        {checkbox: true, fixed: true}
                        , {field: 'id', title: 'ID', width: 80}
                        , {field: 'display_name', title: '平台名称'}
                        , {field: 'name', title: '平台标记'}
                        , {field: 'key', title: 'Key'}
                        , {field: 'created_at', title: '创建时间', sort: true}
                        , {field: 'updated_at', title: '更新时间'}
                        , {fixed: 'right', width: 260, align: 'center', toolbar: '#options'}
                    ]]
                    , done: function (res, curr, count) {
                        layer.close(load);
                    }
                });

                //监听工具条
                table.on('tool(dataTable)', function (obj) { //注：tool是工具条事件名，dataTable是table原始容器的属性 lay-filter="对应的值"
                    var data = obj.data //获得当前行数据
                        , layEvent = obj.event; //获得 lay-event 对应的值
                    if (layEvent === 'regain') {
                        layer.prompt({
                            id: data.id,
                            title: '敏感信息，请输入登入密码',
                            formType: 1,
                            offset: 'auto'
                        }, function (pass, index) {
                            http.post("{{route('platform.platform.checkPassword')}}", {"password": pass}, function (res) {
                                http.post("{{route('platform.platform.secret')}}", {"id": data.id}, function (res) {
                                    layer.alert(res.datas, {icon: 6, title: 'secret生成成功请查收'});
                                })
                            })
                        });
                    } else if (layEvent === 'edit') {
                        layerIfram('/admin/platform/' + data.id + '/edit', '更新平台信息', 493, 350);
                    }
                });

                //搜索点击事件
                $("#search").click(function () {
                    where.like.display_name = '%' + $("#display_name").val() + '%';
                    load = layer.load();
                    dataTable.reload();
                })

                //监听排序
                table.on('sort(dataTable)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                    where.sorts = {};
                    where.sorts[obj.field] = obj.type;
                    load = layer.load();
                    dataTable.reload();
                });

                //按钮批量删除
                $("#listDelete").click(function () {
                    var ids = []
                    var hasCheck = table.checkStatus('dataTable')
                    var hasCheckData = hasCheck.data
                    if (hasCheckData.length > 0) {
                        $.each(hasCheckData, function (index, element) {
                            ids.push(element.id)
                        })
                    }
                    if (ids.length > 0) {
                        layer.confirm('确认删除吗？', function (index) {
                            $.post("{{ route('platform.platform.destroy') }}", {
                                _method: 'delete',
                                ids: ids
                            }, function (result) {
                                if (result.code == 0) {
                                    dataTable.reload()
                                }
                                layer.close(index);
                                layer.msg(result.msg, {icon: 6})
                            });
                        })
                    } else {
                        layer.msg('请选择删除项', {icon: 5})
                    }
                })

                //添加角色按钮
                $("#addrole").click(function () {
                    layerIfram('{{ route('platform.platform.create') }}', '创建平台', 493, 350);
                })

                //封装弹出层
                function layerIfram(url, title, w = 493, h = 580) {
                    var iframlayer = layer.open({
                        type: 2,
                        title: title,
                        shadeClose: true,
                        shade: false,
                        area: ['500px', '500px'],
                        offset: 'auto',
                        content: url,
                        success: function (layero, index) {
                            layer.iframeAuto(index);
                        }
                    });
                    return iframlayer;
                }
            })
        </script>
    @endcan
@endsection